<?php
declare (strict_types = 1);

namespace app\common\service;

use app\common\model\StudyStudentAction;
use think\facade\Request;
use app\common\model\StudentActionNurse as M;
use app\common\model\StudyStudentList;
use app\common\model\StudyStudentLeave;
use app\common\validate\StudentActionNurse as V;

class StudentActionNurse
{
    // 添加
    public static function goAdd($data)
    {
      if (\request()->isAjax()) {
           $student_id = input('param.student_id', '');
           if ($student_id) {
               $student_id = input('param.student_id');
               $student[] = $student_id;
           } else {
               $class_id = input('param.class_id');
               $child = StudyStudentList::where('class_id', $class_id)->where('end_time', '>=', time())->select();
               $student = [];
               foreach ($child as $key => $value) {
                   $is_leave = StudyStudentLeave::where('student_id', $value['id'])
                       ->where('start_time', '<', time())
                       ->where('end_time', '>', time())
                       ->find();
                   if (!$is_leave) {
                       $student[] = $value['id'];
                   }
               }
           }

          $add_time = input('param.add_time');
          $admin_id = session('admin.id');
          $nurse_type = input('param.nurse_type', '');
          $diaper_num = input('param.diaper_num', 0);
          $action_info = input('param.action_info', '');
          foreach ($student as $v) {
              $id = \app\common\model\StudentActionNurse::insertGetId([
                  'student_id' => $v,
                  'add_time' => strtotime($add_time),
                  'admin_id' => $admin_id,
                  'nurse_type' => $nurse_type,
                  'diaper_num' => $diaper_num,
                  'action_info' => $action_info,
              ]);
              StudyStudentAction::create([
                  'student_id' => $v,
                  'action_type' => 3,
                  'action_id' => $id,
                  'add_time' => time(),
                  'admin_id' => $admin_id,
              ]);
          }



       }
    }
    
    // 编辑
    public static function goEdit($data,$id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
             M::update($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save([
                'status' => $data,
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 删除
    public static function goRemove($id)
    {
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
           $model->delete();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids)
    {
        if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
        try{
            M::destroy($ids);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()){
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');
        
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
}
